package com.saer.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.saer.client.dto.UserInfoDto;
import com.saer.client.dto.login.LoginDto;
import com.saer.client.form.login.LoginForm;
import com.saer.client.label.TextLabel;
import com.saer.client.layout.MainLayout;
import com.saer.client.menu.MenuBar;
import com.saer.client.tab.visita.VisitaTab;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SaerEntryPoint implements EntryPoint {
	private static RemoteWebServiceAsync remoteWebService = GWT.create(RemoteWebService.class); 
	private static UserInfoDto userInfo = null;
	private static LoginDto login = null;
	private static boolean isFirstLoad = true;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		remoteWebService.esperaVisita(new EsperaVisitaCallBack());
		loadLoginScreen();
	}

	public static RemoteWebServiceAsync getRemotewebservice() {
		return remoteWebService;
	}

	public static UserInfoDto getUserInfo() {
		return userInfo;
	}
	
	public static LoginDto getLogin() {
		return login;
	}

	public static void setLogin(LoginDto loginDto) {
		login = loginDto;
	}

	public static void loadLoginScreen() {
		if (isFirstLoad) {
			DOM.removeChild(RootPanel.getBodyElement(),	DOM.getElementById("loadingWrapper"));
			isFirstLoad = false;
		}
		
		// Use RootPanel.get() to get the entire body element
		RootPanel rootPanel = RootPanel.get();
		
		rootPanel.clear();

		HLayout loginLayout = new HLayout();
		loginLayout.setHeight100();
		loginLayout.setWidth100();
		loginLayout.setLayoutAlign(Alignment.CENTER);
		loginLayout.setAlign(Alignment.CENTER);
		
		LoginForm loginForm = new LoginForm();
		loginForm.setHeight(390);
		loginForm.setWidth(295);
		loginForm.setLayoutAlign(Alignment.CENTER);
		
		loginLayout.addMember(loginForm);
		
		rootPanel.add(loginLayout, 0, 0);

	}
	
	public static void loadMainScreen() {
		
		// Use RootPanel.get() to get the entire body element
		RootPanel rootPanel = RootPanel.get();

		rootPanel.clear();
		
		MainLayout mainLayout = new MainLayout();
		MenuBar menuBar = new MenuBar(mainLayout);

		VLayout mainVStack = new VLayout();

		mainVStack.setHeight("100%");
		mainVStack.setWidth100();
		
		Img saerHeader = new Img("header.png");
		saerHeader.setHeight(62);
		saerHeader.setWidth(600);
		HLayout headerLayout = new HLayout(); 
		headerLayout.setSize("100%", "62px");
		headerLayout.addMember(saerHeader);

		TextLabel labelFooter = new TextLabel("&reg; Copyright 2012 Universidad Nacional de San Martin / Universidad CAECE");
		labelFooter.setWidth("100%");
		labelFooter.setAlign(Alignment.CENTER);

		HLayout footerLayout = new HLayout(); 
		footerLayout.setWidth100();
		footerLayout.setHeight(25);
		footerLayout.addMember(labelFooter);
		
		mainVStack.addMember(headerLayout);
		mainVStack.addMember(menuBar);
		mainVStack.addMember(mainLayout);
		mainVStack.addMember(footerLayout);

		rootPanel.add(mainVStack, 0, 0);
		
		mainLayout.addTab(new VisitaTab(mainLayout));
		mainLayout.setHasVisitaTab(true);
	}
	
	private class EsperaVisitaCallBack implements AsyncCallback<Void> {
		
		public void onFailure(Throwable arg0) {
			SC.say("Error esperando visitas", "[ERROR]");
		}

		public void onSuccess(Void result) {

		}
	}
	
	public static native void closeBrowserWindow()
	/*-{
		$wnd.close();
	}-*/;
}
